<?php

ini_set('max_execution_time', 6000);
include_once dirname(dirname(dirname(__FILE__))) . '/include/common.php';
include_once 'simple_html_dom.php';
include_once dirname(dirname(dirname(__FILE__))) . '/include/core/image.php';
$core = new core();
//get and save url navagation
$html = new simple_html_dom();
// Load HTML from a URL
$begin = time();

function get_content_html($url) {

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 300);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, false);


    curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookies_es.txt');
    curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookies_es.txt');

    curl_setopt($curl, CURLOPT_COOKIESESSION, TRUE);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8');

    $result['EXE'] = curl_exec($curl);
    $result['INF'] = curl_getinfo($curl);
    $result['ERR'] = curl_error($curl);
    curl_close($curl);
    return $result;
}

function get_url_product() {
    $core = new core();
    $query = "SELECT * FROM zen_crawl_product WHERE is_update_pt =0  ORDER BY id ASC LIMIT 1 ";
    // $query = "SELECT * FROM zen_crawl_url_product WHERE id=249 ";
    $list_nav = $core->db->get_row($query);
    if($list_nav ==null){
        return false;
    }
    return $list_nav;
}

echo '<table><tr><th>url</th><th>product name</th><th>product code</th><th>error</th></tr>';
while ($product = get_url_product()) {
    if (time() - $begin > 180) {
        die("het 3 phut running!!!");
    }
    $core->db->query("UPDATE zen_crawl_product SET is_update_pt=1 WHERE id=$product->id");
    $link_web = str_replace('/fr/','/es/',$product->link_web);
   // $link_web = $product->link_web;
    //$render_content.='<tr colspan="4"><td>Crawl child menu ' . $level . ': '.$product->url_product.'</td></tr>';
    echo '<tr colspan="4"><td>Crawl url product  ' . $product->product_name . ': ' . $link_web . '</td></tr>';
    $time_running = time() - $begin;
    echo '<tr colspan="4"><td>time running = ' . $time_running . ', minute=' . ($time_running / 60) . '</td></tr>';

    $data_html = get_content_html($link_web);
    if ($data_html['ERR'] != '') {
        echo '<tr><td></td><td></td><td></td><td>loi crawl 01:' . $data_html['ERR'] . ' </td></tr>';
        $core->db->query("UPDATE zen_crawl_product SET  note='loi crawl 01: ". $data_html['ERR'] . "' WHERE id=$product->id");
       die;
    }
    $html->load($data_html['EXE']);

    $repeat = 0;
    while (!$html->find('#product', 0)) {
        $repeat++;
        if ($repeat > 2) {//khong tim thay san pham->loai bo
            $core->db->query("UPDATE zen_crawl_product SET is_update_pt=2, note='Khong tim thay san pham o link: ".json_encode($data_html['EXE'])."' WHERE id=$product->id");
            break;
        }
        //$render_content.='<tr><td></td><td></td><td></td><td>Khong tim thay menu </td></tr>';
        echo '<tr><td></td><td></td><td></td><td>Khong tim thay san pham '.json_encode($data_html['EXE']).'</td></tr>';
        sleep(2);
        $data_html = get_content_html($link_web);
        if ($data_html['ERR'] != '') {
            echo '<tr><td></td><td></td><td></td><td>loi crawl 02:' . $data_html['ERR'] . ' </td></tr>';
            $core->db->query("UPDATE zen_crawl_product SET  note='loi crawl 02: ".$data_html['ERR']."' WHERE id=$product->id");
           die;
        }
        $html->load($data_html['EXE']);
    }
    if ($repeat > 2) {
        continue;
    }


    $product_html = $html->find('#product', 0);
    $product_data = array();

    //get product's infomation

    if ($product_html->find('div[class=right]', 0)) {
        $div_right = $product_html->find('div[class=right]', 0);


        //get product price
        if ($div_right->find('p[class=price] span[class=price]', 0)) {
            $tag_span_price = $div_right->find('p[class=price] span[class=price]', 0);
            $html_price = $tag_span_price->outertext; //<span class="price" data-price="17.95 EUR">   </span>
            $pos_last_equal = strripos($html_price, '=');
            $pos_last_eur = strripos($html_price, 'EUR');
            $product_data['product_store_price'] = floatval(trim(substr($html_price, $pos_last_equal + 2, (($pos_last_eur - 2) - $pos_last_equal))));

            if ($product_data['product_store_price'] == '') {
                echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Khong tim thay PRODUCT PRICE</td></tr>';
                $core->db->query("UPDATE zen_crawl_product SET  note='Khong tim thay PRODUCT PRICE' WHERE id=$product->id");
                continue;
            }
        }elseif($div_right->find('p[class=price] span[class=sale]', 0)){
            $tag_span_price = $div_right->find('p[class=price] span[class=sale]', 0);
            $html_price = $tag_span_price->outertext; //<span class="price" data-price="17.95 EUR">   </span>
            $pos_last_equal = strripos($html_price, '=');
            $pos_last_eur = strripos($html_price, 'EUR');
            $product_data['product_store_price'] = floatval(trim(substr($html_price, $pos_last_equal + 2, (($pos_last_eur - 2) - $pos_last_equal))));

            if ($product_data['product_store_price'] == '') {
                echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Khong tim thay PRODUCT PRICE</td></tr>';
                $core->db->query("UPDATE zen_crawl_product SET  note='Khong tim thay PRODUCT PRICE 03' WHERE id=$product->id");
                continue;
            }
        } else {
            echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Khong tim thay PRODUCT PRICE</td></tr>';
            $core->db->query("UPDATE zen_crawl_product SET  note='Khong tim thay PRODUCT PRICE 02' WHERE id=$product->id");
            continue;
        }


    } else {
        echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Khong tim thay DIV RIGHT</td></tr>';
        $core->db->query("UPDATE zen_crawl_product SET  note='Khong tim thay DIV RIGHT' WHERE id=$product->id");
        continue;
    }
    echo '<tr><td></td><td></td><td></td><td>stamp_price = '.$product->stamp_price.'  and store price:' . $product_data['product_store_price'] . ' </td></tr>';
    $core->db->query("UPDATE zen_crawl_product SET is_update_pt=3, store_price=".$product_data['product_store_price'].", link_web ='".$link_web."' WHERE id=$product->id");
    sleep(2);
    //die("stop");

}

//$render_content.='</table>';
echo '</table>';
//echo $render_content;
die;
?>
